// pages/convenient/convenient.js
var Bmob = require('../../utils/Bmob-2.2.0.min.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
  franchisee:[],
  inputShowed: false,
  inputVal: "",
  height:0,
    categoryList:[],
    categoryActive: 11,
    getSectionData: {
      page: 0,
      loading: false,
      nomore: false
    },
    isGetShop:false,
    shopId:'',
    status:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var categoryActive = Number(options.type) || 0;
    this.getheight();
    this.categoryList();
   
  },
  onShareAppMessage: function () {

  },
  
  getheight:function(){
    var that=this;
    wx.getSystemInfo({
     success:function(res) {
      
      that.setData({

        height: res.windowHeight-46

      })
     


    }
  })


  },
  turnToPublish: function (event) {
    var that=this;
    var isGetShop = that.data.isGetShop;
    var shopId = that.data.shopId;
    var state=that.data.state;
    app.turnToPage('/pages/franchiseeEnterStatus/franchiseeEnterStatus');
    /*
    if (shopId && isGetShop)
      app.turnToPage('/pages/franchiseePerfect/franchiseePerfect?franchisee_id=' + shopId);
    if(!shopId)
      wx.showToast({
        title: '未申请店铺',
        icon: 'fail',
        duration: 2000
      })
   */
  },

turnToCooperation: function(event) {

  app.turnToPage('/pages/franchiseeCooperation/franchiseeCooperation');
  },
  turnToAdd: function (event) {
    var that = this;
    var isGetShop = that.data.isGetShop;
    var shopId = that.data.shopId;
    app.turnToPage('/pages/franchiseeEnterStatus/franchiseeEnterStatus');
/*
    if(!shopId)
    app.turnToPage('/pages/franchiseeEnter/franchiseeEnter');
    else
      wx.showToast({
        title: '已申请',
        icon: 'fail',
        duration: 2000
      })
      */
  },
  clickOrderTab: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.id;
    this.setData({
      categoryActive: index,
      goodsList: [],
      getSectionData: {
        page: 1,
        loading: false,
        nomore: false
      }
    })
    this.get_franchiseeList();
  },
  categoryList: function () {
    var that = this;   
    var categoryList = Bmob.Query("categroyList");   
    var franchisee = Bmob.Pointer("Franchisee");
    var franchiseeId = franchisee.set("86cyg11I"); 
 
    categoryList.equalTo("franchisee","==", franchiseeId);
    categoryList.find().then(data=>{
     console.log(data);
        var categoryList = that.data.categoryList;
        for (var i = 0; i < data.length; i++) {
          var category = { category: 0, name: '全部' }
          category.category = data[i].categroy;
          category.name = data[i].name;
          categoryList.push(category)
        }
        var data = {};
        data.categoryList = categoryList
        that.setData(data);
    })

    this.get_franchiseeList();
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });

  },
  tapPhoneCallHandler: function (e) {

    app.tapPhoneCallHandler(e);
  },
  hideInput: function () {
    var that=this;
    this.setData({
      inputVal: "",
      inputShowed: false
    });
that.get_franchiseeList();
  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },

  search:function(e){
    var that = this;
    var search_str=that.data.inputVal;
   
    var franchisee = new Bmob.Query("Franchisee");
   
    franchisee.equalTo("state","==", true);
    franchisee.equalTo("Franchisee_title","==",search_str);
    franchisee.order('-createdAt');  //排序

    franchisee.find().then(res=>{

        //var goods=res;

        that.setData({

          franchisee: res

        })

     
    });

  },
  turnTofranchiseeDetail: function (e) {
   console.log(e)
   var id=e.currentTarget.dataset.id;
   var franchiseetype = e.currentTarget.dataset.type;
   
    wx.navigateTo({
      url: "../../pages/franchiseeDetail/franchiseeDetail?detail=" + id +"&type="+franchiseetype
   })
  },

  get_franchiseeList: function (param) {
    var that = this;
  
    var franchisee =  Bmob.Query("Franchisee");
    var categoryActive = that.data.categoryActive;
    franchisee.equalTo("Franchisee_type","==", categoryActive);
    franchisee.equalTo("state","==", true);
    franchisee.order('-createdAt');  //排序

    franchisee.find().then(res=>{
    
        that.setData({

          franchisee: res

        })
   
    });


  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})